package math
{
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.BlurFilter;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	
	/**
	 * 玫瑰线
	 * 
	 * 公式:
	 * p=a *sin(n*angle) n为花瓣
	 * p=a *cos(n*angle)n为花瓣
	 * */
		public class Roseline extends Sprite
		{
			private var angle:Number=0;
			/** 花瓣数*/
			private var n:int = 1;
			public function Roseline(centerX:Number, centerY:Number, stage:Stage)
			{
				this.x = centerX;
				this.y = centerY;
				init(stage);
			}
			
			private function init(stage:Stage):void
			{
				this.graphics.moveTo(0, 0);
				stage.addEventListener(Event.ENTER_FRAME,Run);
				stage.addEventListener(MouseEvent.CLICK,clickHandler);
			}
			
			private function Run(event:Event):void
			{
				angle++;
				this.drawRose(200,angle);
			}
			
			private function clickHandler(evt:MouseEvent):void
			{
				this.graphics.clear();
				n++;
				var glow:GlowFilter = new GlowFilter(); 
				glow.color = 0xFFFFFF * Math.random(); 
				glow.alpha = 1; 
				glow.blurX = 25; 
				glow.blurY = 25; 
				glow.quality = BitmapFilterQuality.MEDIUM; 
				
				this.filters = [glow];
			}
			
			/** 创建2D的玫瑰线*/
			public function CreatRose(r:Number,num:int):void
			{
				var rad:Number=1*Math.PI/180;//角度
				for (var j:int=0; j<=360; j++)
				{
					var len:Number=r*Math.sin(num*rad*j);
					var point:Point=Point.polar(len,rad*j);
					drawPoint(point);
				}
			}
			
			/** 绘制玫瑰线*/
			public function drawRose(r:Number,angle:Number):void
			{
				var len:Number=r*Math.sin(n*angle);
				var point:Point=Point.polar(len,angle);
				drawPoint(point);
			}
			
			/** 描点*/
			private function drawPoint(point:Point):void
			{
				this.graphics.lineStyle(1, 0xffffff);
				this.graphics.lineTo(point.x,point.y);
			}
		}
	}
